﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.XSchEmA;
using Pixysoft.Tools;

namespace Pixysoft.Framework.Noebe.Orm.testdriven
{
    class quickstart
    {
        /// <summary>
        /// 部署数据库
        /// </summary>
        public void test_001()
        {
            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory) + "helloworld.mdb";

            XSchemaManager.AccessSchema.Schema.DropDatabase(null, path);
            XSchemaManager.AccessSchema.Schema.CreateDatabase(null, path);

            NoebeConfiguration config = new NoebeConfiguration();
            config.DatabaseType = DatabaseType.Access;
            config.Filename = path;
            NoebeManager.Instances[OrmManager.GLOBAL_FRAMEWORK_ID].Reloads(config);
            NoebeManagerAdapter.Instances[OrmManager.GLOBAL_FRAMEWORK_ID].Initializes(NoebeManager.Instances[OrmManager.GLOBAL_FRAMEWORK_ID]);

            new OrmSchema().Drop<Person>(config);
            new OrmSchema().Commit<Person>(config);
        }

        /// <summary>
        /// 增删改查
        /// </summary>
        public void test_002()
        {
            //初始化ORM依赖的持久层

            string path = StringFilter.RegularPath(AppDomain.CurrentDomain.BaseDirectory) + "helloworld.mdb";
            NoebeConfiguration config = new NoebeConfiguration();
            config.DatabaseType = DatabaseType.Access;
            config.Filename = path;
            NoebeManager.Instances[OrmManager.GLOBAL_FRAMEWORK_ID].Reloads(config);
            NoebeManagerAdapter.Instances[OrmManager.GLOBAL_FRAMEWORK_ID].Initializes(NoebeManager.Instances[OrmManager.GLOBAL_FRAMEWORK_ID]);



            //创建对象

            string carpk = GlobalTimer.Instance.GetGlobalTimeSequence();
            Car car = OrmManager.Instance.GetSession().Create<Car>();
            car.StringValue = carpk;
            OrmManager.Instance.GetSession().Save(car);

            List<string> list = new List<string>();
            list.Add("1");
            list.Add("2");
            list.Add("3");

            string personpk = GlobalTimer.Instance.GetGlobalTimeSequence();
            Person person = OrmManager.Instance.GetSession().Create<Person>();
            person.StringValue = personpk;
            person.IntValue = 13;
            person.StringArray = new string[] { "1", "2", "3" };
            person.StringList = list;

            person.OrmObject = car;
            person.OrmArray = new Car[] { car };
            List<Car> ormlist = new List<Car>();
            ormlist.Add(car);
            person.OrmList = ormlist;
            OrmManager.Instance.GetSession().Save(person);


            //简易查询

            person = OrmManager.Instance.GetQuery().SelectByUpk<Person>(personpk);
            Console.WriteLine(person.IntValue);



            //右查询 从Person开始查，查询条件是Person.OrmList的所有值。

            Console.WriteLine("------------ RIGHT QUERY ------------");
            IOrmRightQuery<Person> rightquery = OrmManager.Instance.GetRightQuery<Person>();//第一个范型表示查询起始的对象类型
            rightquery.QueryString = "OrmList";
            rightquery.OrmObject = person;
            foreach (Car qcar in rightquery.Select<Car>()) //第二个范型表示查询结果的类型
            {
                Console.WriteLine(qcar.StringValue);
            }


            //左查询 从car开始向左查询，得到拥有这个Car的Person集合。

            Console.WriteLine("------------ LEFT QUERY ------------");
            IOrmLeftQuery<Person> leftquery = OrmManager.Instance.GetLeftQuery<Person>();
            leftquery.QueryString = "OrmList.StringValue = :VALUE";
            leftquery.Parameters.Add("VALUE", carpk);
            foreach (Person qperson in leftquery.Select())
            {
                Console.WriteLine(qperson.StringValue);
            }
        }
    }
}
